package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import mx.graphics.codec.JPEGEncoder;
	import mx.graphics.codec.PNGEncoder;
	
	import spark.components.Label;
	
	/**
	 * flash的assets转换成laya的assets，swf只打包一个主目录，那么到laya里想办法把 主目录下的子目录都打包成一个 图集。
	 */
	public class AssetsToLaya
	{
		private static var _ins:AssetsToLaya;
		public static function get ins():AssetsToLaya
		{
			if(!_ins){
				_ins = new AssetsToLaya();
			}
			return _ins;
		}
		
		private var _loader:Loader;
		private var _path:String;
		public var resType:int = 1;
		
		public static var exportUrl:String;
		public static var importRoot:String;
		public static var printMsg:Label;
		
		private var _isJpeg:Boolean;
		
		public function AssetsToLaya()
		{
		}
		public function doLoad(path:String):void
		{
			_path = path;
			//			var path:String = "29.mp";
			var request:URLRequest = new URLRequest(path);
			if(!_loader) {
				_loader = new Loader();
				_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
				_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
				//				_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
				//				_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError);
				//				_loader.addEventListener(Event.OPEN, onLoadOpen);
			}
			_loader.load(request);
		}
		private function onProgress(e:Event):void
		{
			
		}
		private function onComplete(e:Event):void
		{
			if(_loader.contentLoaderInfo.contentType.indexOf("jpeg") != -1){
				_isJpeg = true;	
			}
			else{
				_isJpeg = false;	
			}
			
			writeImg(_path);
		}
		private function onLoadError(e:Event):void
		{
			
		}
		private function onStatus(e:Event):void
		{
			
		}
		private function onLoadOpen(e:Event):void
		{
			
		}
		
		//------------output=------------------------
		public function writeImg(path:String):void
		{
			var path:String = _path.replace(importRoot, "");
			if(path.indexOf("\\") == 0){
				path = path.substring(1);
			}
			
			//将D:\work\qixia\code\mhj_h5\HHZZ_Release\laya\assets\mobile\a_common_v1\vip\vip2_1.png
			//处理成：D:\work\qixia\code\mhj_h5\HHZZ_Release\laya\assets\mobile\a_common_v1\vip&vip2_1.png
			if(path.indexOf("a_test\\") == -1){
				var arr:Array = path.split("mobile\\");
				if(arr[1]){
					var arr2:Array = arr[1].split("\\");
					var newpath:String = arr[0] + "mobile\\";
					for(var i:int=0; i<arr2.length; i++){
						if(i == 0){
							newpath += arr2[i] + "\\";
						}
						else if(i < arr2.length-1){
							newpath += arr2[i] + "&";
						}
						else{
							newpath += arr2[i];
						}
					}
				}
				else{
					newpath = path;
				}
			}
			else{
				newpath = path;
			}
			
			var oldF:File = new File(importRoot);
			var oldF2:File = oldF.resolvePath(path);
			
			var newF:File = new File(exportUrl);
			var newF2:File = newF.resolvePath(newpath);
			
			oldF2.copyTo(newF2, true);
			
			
//			printMsg.text = "正在转换，path=" + path;
		}
	}
}